<?php 

class Api extends CI_Controller {

	public function StudentRegisterApi() {
		$data['nickname'] = $_REQUEST['NickName'];
		$data['mail'] = $_REQUEST['Email'];
		$data['password'] = $_REQUEST['Password'];

		$state = $this->db->insert('student', $data);
		if( $state ) {
			$stateInfo['Session'] = LfDealString::GenerateStr();
			$stateInfo['State'] = "1";
			echo json_encode($stateInfo);
		} else {
			$stateInfo['State'] = "0";
			echo json_encode($stateInfo);
		}
	}

	public function StudentLoginApi() {
		$mail = $_REQUEST['Email'];
		$password = $_REQUEST['Password'];

		$studentInfo = $this->db->select('Id', 'HasGetFreeLesson')->get_where('student', array('Mail'=>$mail))->result_array();;

		$data['Session'] = LfDealString::GenerateStr();
		$data['StudentId'] = $studentInfo['Id'];
		$data['HasGetFreeLesson'] = $studentInfo['HasGetFreeLesson'];
		$data['LoginState'] = 1;
		echo json_encode($data);
	}

	public function EditStudentProfileApi() {
		$StudentId = $_REQUEST['StudentId'];

		$data['Nation'] = $_REQUEST['Nation'];
		$data['PhoneNumber'] = $_REQUEST['PhoneNumber'];
		$data['Age'] = $_REQUEST['Age'];
		$data['Gender'] = $_REQUEST['Gender'];
		$data['Purpose'] = $_REQUEST['Purpose'];
		$data['Belongs'] = $_REQUEST['Belongs'];

		$state = $this->db->where('Id', $StudentId)->update('student', $data);
		if( $state ) {
			$state['State'] = 1;
			echo json_encode($state);
		} else {
			$state['State'] = 0;
			echo json_encode($state);
		}
	}
}
